package com.it.flowable.model;

import com.it.flowable.util.DataUtils;
import com.it.flowable.util.Utils;
import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;

public class GatewayModel {

    // 流程常量
    public static final String PROCESS_KEY = "GatewayModel";
    public static final String PROCESS_NAME = "GatewayModel";

    // 节点ID
    public static final String START_EVENT = "start";
    public static final String APPLY_TASK = "applyTask";
    public static final String LEADER_APPROVAL = "leaderApproval";
    public static final String END_EVENT = "end";
    public static final String LEADER_DECISION = "leaderDecision";

    /**
     * 创建简化的请假审批流程
     */
    public static BpmnModel create() {
        BpmnModel model = new BpmnModel();
        Process process = new Process();
        process.setId(PROCESS_KEY);
        process.setName(PROCESS_NAME);
        model.addProcess(process);

        // 创建并添加所有节点
        Event startEvent = Utils.createStartEvent(START_EVENT, "开始事件", null, null, null, null);
        UserTask submitTask = Utils.createUserTask(APPLY_TASK, "填写请假申请", "${initiator}", "APPLY", DataUtils.createSubmitFormProperties(), null, null);
        UserTask leaderTask = Utils.createUserTask(LEADER_APPROVAL, "直属领导审批", "${leaderAssignee}", "APPROVAL", DataUtils.createApprovalFormProperties(), null, null);
        Event endEvent = Utils.createEndEvent(END_EVENT, "结束事件");

        // 网关
        ExclusiveGateway exclusiveGateway = Utils.createExclusiveGateway(LEADER_DECISION, "领导审批决策");
        process.addFlowElement(exclusiveGateway);

        // 创建序列流
        SequenceFlow flow = Utils.createSequenceFlow(START_EVENT, APPLY_TASK, "提交申请", null, null);
        SequenceFlow flow1 = Utils.createSequenceFlow(APPLY_TASK, LEADER_APPROVAL, "提交审批", null, null);
        SequenceFlow flow2 = Utils.createSequenceFlow(LEADER_APPROVAL, LEADER_DECISION, null, null, null);
        // 审批决策
        SequenceFlow rejectFlow = Utils.createSequenceFlow(LEADER_DECISION, APPLY_TASK, "审批驳回", "${approveResult == 'REJECT'}", null);
        SequenceFlow approvedFlow = Utils.createSequenceFlow(LEADER_DECISION, END_EVENT, "审批通过", "${approveResult == 'APPROVE'}", null);

        process.addFlowElement(startEvent);
        process.addFlowElement(submitTask);
        process.addFlowElement(leaderTask);
        process.addFlowElement(endEvent);

        process.addFlowElement(flow);
        process.addFlowElement(flow1);
        process.addFlowElement(flow2);

        process.addFlowElement(rejectFlow);
        process.addFlowElement(approvedFlow);

        return model;
    }

    // ========== 表单相关方法 ==========

}